package com.bandaotixi.cashier.api.system.service;

import cn.hutool.core.collection.CollectionUtil;
import com.bandaotixi.cashier.api.core.mybatis.SqlUtil;
import com.bandaotixi.cashier.api.system.mapper.RoleMapper;
import com.bandaotixi.cashier.api.system.model.entity.Role;
import com.github.yulichang.base.MPJBaseServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
@RequiredArgsConstructor
public class RoleService extends MPJBaseServiceImpl<RoleMapper, Role> {

    @Transactional
    public void assignMenusToRole(Long roleId, List<Long> menuIds) {
        Role role = this.getById(roleId);
        if (role == null) {
            throw new RuntimeException("角色不存在");
        }
        // 删除角色菜单
        SqlUtil.delete("sys_role_menu", Map.of("role_id", roleId));
        // 新增角色菜单
        if (CollectionUtil.isNotEmpty(menuIds)) {
            SqlUtil.batchUpsert("sys_role_menu", menuIds.stream().map(menuId -> Map.of("role_id", (Object) roleId, "menu_id", menuId)).toList());
        }
    }
}




